package poker.ai;

import java.util.ArrayList;

import poker.*;
import enums.*;

public class MediumAI extends Player
{

	boolean playerTypeConservative;
	
	boolean conservative;
	
	public MediumAI(int startCash, String name, boolean conservative) 
	{
		super(startCash, name);
		playerTypeConservative = conservative;
		this.conservative = conservative;
		
	}

	@Override
	public ActionType chooseAction(GameRound gameRound) 
	{
		double otherFactors = 0;
		
		if(!conservative){ otherFactors = 0.1;} //Liberal player
		
		ArrayList<ActionType> legalActions = Rules.getLegalActions(this);
		Printer.printAvailbleActions(this, legalActions);
		Printer.printValueToCall(this, gameRound.getCurrentBet() - getPostedCash());
		int playersLeft = gameRound.getPlayersLeft();
		
		PlayersLeft pLeft = PlayersLeft.get(playersLeft);
		int position = gameRound.getPosition(this) - gameRound.getDealerPosition() % gameRound.getPlayers().size();
		TablePosition tPosition = TablePosition.get(position);
		
		double handStrength;
		if(gameRound.getIsPreFlop())
			handStrength = HandStrength.getHandStrengthPreFlop(getPocketCards(), playersLeft);
		else
			handStrength = HandStrength.calculateHandStrength(getPocketCards(), gameRound.getCommunityCards(), playersLeft);
		
		double potOdds = getPotOdds(gameRound);
		

		switch (tPosition) 
		{
			case early:
				if(handStrength > potOdds)
					otherFactors += .01;
			break;
			case med:
				if(handStrength > potOdds)
					otherFactors += .02;
				else
					otherFactors += .01;
			break;
			case late:
				if(handStrength > potOdds)
					otherFactors += .04;
				else
					otherFactors += .02;
			break;
		}
		
		ActionType actionType = ActionType.FOLD;
		
		switch(pLeft)
		{
		case few:
			//A raise hand
			//Note: små endringer har veldig sort utslag
			if(handStrength + otherFactors > 0.47)
			{
				if(legalActions.contains(ActionType.RAISE) || legalActions.contains(ActionType.BET))
					actionType = ActionType.RAISE;
				else
					actionType = ActionType.CALL;
			}
			//A call hand
			else if(handStrength + otherFactors > 0.25)
			{
				if(legalActions.contains(ActionType.CHECK))
					actionType = ActionType.CHECK;
				else
					actionType = ActionType.CALL;
			}
			//A fold hand
			else
			{
				if(legalActions.contains(ActionType.CHECK))
					actionType = ActionType.CHECK;
				else
					actionType = ActionType.FOLD;
			}
			
			break;
		case some:
			//A raise hand
			if(handStrength + otherFactors > 0.22)
			{
				if(legalActions.contains(ActionType.RAISE) || legalActions.contains(ActionType.BET))
					actionType = ActionType.RAISE;
				else
					actionType = ActionType.CALL;
			}
			//A call hand
			else if(handStrength + otherFactors > 0.19)
			{
				if(legalActions.contains(ActionType.CHECK))
					actionType = ActionType.CHECK;
				else
					actionType = ActionType.CALL;
			}
			//A fold hand
			else
			{
				if(legalActions.contains(ActionType.CHECK))
					actionType = ActionType.CHECK;
				else
					actionType = ActionType.FOLD;
			}
			break;
		case many:
			//A raise hand
			if(handStrength + otherFactors > 0.14)
			{
				if(legalActions.contains(ActionType.RAISE) || legalActions.contains(ActionType.BET))
					actionType = ActionType.RAISE;
				else
					actionType = ActionType.CALL;
			}
			//A call hand
			else if(handStrength + otherFactors > 0.11)
			{
				if(legalActions.contains(ActionType.CHECK))
					actionType = ActionType.CHECK;
				else
					actionType = ActionType.CALL;
			}
			//A fold hand
			else
			{
				if(legalActions.contains(ActionType.CHECK))
					actionType = ActionType.CHECK;
				else 
					actionType = ActionType.FOLD;
			}
			break;
		}
			
		return actionType;
	}
	
}
